<template>
  <div>
    <div v-if="type!=='detail'" style="margin-bottom: 10px;" class="btn-group">
      <div>
        <el-button type="primary" @click="add">新增</el-button>
      </div>
    </div>
    <lb-table
      ref="lbTable"
      class="tableOrder"
      highlight-current-row
      max-height="400"
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      height="200"
      :pagination="false"
      :total="tableData.total"
    />
    <tableUsers ref="tableUsers" :selected-table="tableData.data" @getchek="getchek" />
  </div>
</template>

<script>

  export default {
    name: 'peisongUser',
    components: {
      tableUsers: require('./tableUsers.vue').default
    },
    props: {
      type: {
        type: String,
        default: null
      }
    },
    data() {
      return {
        tableData: {
          column: [
            { type: 'index', align: 'center' },
            { label: '状态', prop: 'status', formatter: this.csmStatusFormat },
            { label: '客户编号', prop: 'csmCode' },
            { label: '客户名称', prop: 'csmName' },
            { label: '客户电话', prop: 'csmPhone', width: 120 },
            { label: '地址', prop: 'csmAddress', width: 200 },
            { label: '配送机构', prop: 'orgName', width: 200 },
            { label: '操作', prop: 'sendCount', width: 60,
              render: (h, { row, $index }) => this.type !== 'detail' && <el-link underline={false} type='danger' onClick={() => { this.del(row, $index) }}> 删除</el-link>
            }
          ],
          data: []
        },
        submitTableData: []
      }
    },
    methods: {
      csmStatusFormat: function(row, column) {
        if (row.status === 1) {
          return '未审核'
        } else if (row.status === 2) {
          return '已审核'
        }
      },
      getchek(val) {
        this.tableData.data = val
        this.submitTableData = JSON.parse(JSON.stringify(val))
      },
      del(row, index) {
        const item = this.submitTableData.find(item => item.csmId === row.csmId)
        const idx = this.submitTableData.findIndex(item => item.csmId === row.csmId)
        this.tableData.data.splice(index, 1)
        this.submitTableData.splice(idx, 1, {
          ...item,
          status: -1
        })
      },
      add() {
        this.$refs.tableUsers.open()
      }
    }
  }
</script>

